import React, { Component } from 'react';
import { View, Image, Picker } from '@tarojs/components';
import { AtButton, AtCheckbox, AtForm, AtInput, AtFloatLayout, AtList, AtListItem, AtNavBar } from "taro-ui";
import Taro from '@tarojs/taro';
import "./usersadmin.less";
import config from '../../config.json';


enum authority {
    "管理员" = 2,
    "超级管理员" = 1,
    "一般用户" = 3
}

class UsersAdmin extends Component<any, any> {
    constructor(props) {
        super(props);
        this.state = {
            users: []
        }
    }

    async componentDidMount() {
        let res = await Taro.request({
            url: config.domainName + "/user/queryList",
            header: {
                "token": Taro.getStorageSync("token")
            }
        });
        if (!res.data.code) {
            let users = res.data.data;
            users.sort((a, b) => {
                return a.authority - b.authority;
            })
            this.setState({
                users: res.data.data
            });
        } else {
            Taro.showToast({
                icon: "none",
                title: res.data.msg
            })
        }
    }

    render() {
        return (
            <>
                <AtNavBar
                    onClickLeftIcon={() => { Taro.navigateBack() }}
                    color='#000'
                    title='用户管理'
                    leftIconType="chevron-left"
                />
                <AtList>
                    {this.state.users.map((item) => {
                        if (item.uid !== "root") {
                            return <AtListItem
                                note={item.note}
                                title={item.name}
                                extraText={authority[item.authority]}
                                arrow='right'
                                onClick={() => {
                                    Taro.setStorageSync("user-admin-uid", item.uid);
                                    Taro.setStorageSync("user-admin-authority", item.authority);
                                    Taro.navigateTo({
                                        url: "/pages/useradmindetail/useradmindetail"
                                    });
                                }}
                            />
                        }
                    })}
                </AtList>
            </>
        )
    }
}
export default UsersAdmin;